using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using VK.Comm;

namespace VK.Comm.Cookie
{
    public class CookieSerializer<T> where T : class
    {
        GenericSerializer<T> ser = new GenericSerializer<T>();

        public string SerializeObject(T item)
        {
            string serializeobj;
            IFormatter bf = new BinaryFormatter();
            using (MemoryStream ms = new MemoryStream())
            {
                bf.Serialize(ms, item);
                byte[] byt = new byte[ms.Length];
                byt = ms.ToArray();
                serializeobj = Convert.ToBase64String(byt);
                ms.Flush();
            }
            return serializeobj;
        }

        public T DeserializeObject(string contents)
        {
            T t = null;
            IFormatter bf = new BinaryFormatter();
            byte[] byt = Convert.FromBase64String(contents);

            using (MemoryStream ms = new MemoryStream(byt, 0, byt.Length))
            {
                t = (T)bf.Deserialize(ms);
            }
            return t;
        }
    }
}
